package bookshop.controller.front;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bookshop.domain.Customer;
import bookshop.domain.Manager;
import bookshop.domain.OrderInfo;
import bookshop.domain.OrderTrace;
import bookshop.domain.ReceiveAddr;
import bookshop.domain.ShoppingCart;
import bookshop.service.CustomerService;
import bookshop.service.OrderService;
import bookshop.service.OrderTraceService;
import bookshop.service.ReceiveAddrService;
import bookshop.service.impl.CustomerServiceImpl;
import bookshop.service.impl.OrderServiceImpl;
import bookshop.service.impl.OrderTraceServiceImpl;
import bookshop.service.impl.ReceiveAddrServiceImpl;
import bookshop.utils.OrderNumber;

public class OrderServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		String act = request.getParameter("act");

		if("setOrder".equals(act)){
			Customer customer = (Customer)request.getSession().getAttribute("loginedCustomer");
			String custId=customer.getCustomerId();
			ReceiveAddrService raService = new ReceiveAddrServiceImpl();
			
			List<ReceiveAddr> ras = new ArrayList<ReceiveAddr>();
			ras = raService.GetAllReceiverAddrByCustId(custId);


			request.setAttribute("ras", ras);
			
			
			request.getRequestDispatcher("jsps/front/order/input_order.jsp").forward(request, response);
		}else if("doneOrder".equals(act)){
			Customer customer = (Customer)request.getSession().getAttribute("loginedCustomer");
			String custId=customer.getCustomerId();
			ShoppingCart cart=(ShoppingCart)request.getSession().getAttribute("shoppingcart");
			
			
			OrderInfo orderInfo = new OrderInfo();
			orderInfo.setOrderId(OrderNumber.getOrderNumber());
			orderInfo.setOrderTime(new java.util.Date());
			ReceiveAddrService raService =  new ReceiveAddrServiceImpl();
			orderInfo.setReceiveAddr(raService.GetReceiverAddrById(Integer.parseInt(request.getParameter("receiveId"))));
			orderInfo.setPayMode(request.getParameter("pay_mode"));
			orderInfo.setDeliveryMode(request.getParameter("receive_mode"));
			orderInfo.setOrderAmount(cart.getCartAmount());
			CustomerService cs = new CustomerServiceImpl();
			orderInfo.setCustomer(cs.getCustomerById(custId));
			orderInfo.setOrderStatus("A");
			
			OrderService orderService = new OrderServiceImpl();
			orderService.SavaOrderInfo(orderInfo);
			
			orderService.saveAllOrderItem(cart, orderInfo.getOrderId());	
			
            Manager manager = new Manager();
			
			OrderTrace orderTrace = new OrderTrace();
			orderTrace.setCustomer(customer);
			orderTrace.setManager(manager);
			orderTrace.setOrderId(orderInfo.getOrderId());
			orderTrace.setOrderStatus(orderInfo.getOrderStatus());
			orderTrace.setOperTime(new java.util.Date());
			
			OrderTraceService orderTraceService = new OrderTraceServiceImpl();
			orderTraceService.SaveOrderTrace(orderTrace);
			
			
			request.getSession().removeAttribute("shoppingcart");
			request.setAttribute("orderId", orderInfo.getOrderId());
			request.getRequestDispatcher("jsps/front/order/success_order.jsp").forward(request, response);
		}
	}
 
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}

	
}
